rtgrub=0
if [ $# -eq 0 ]; then
    echo "No arguments, default as Normal"
    echo "===============  Normal Grub  ==============="
else
    if [ $1 = "rt" ]; then
        echo "===================  Real-time Grub  ====================="
        rtgrub=1
    else
        if [ $1 = "normal" ]; then
            echo "===============  Normal Grub  ==============="
        fi
    fi
fi

grub_str_rt=''
grub_str_rt+='GRUB_DEFAULT=\"Advanced options for Ubuntu>Ubuntu, with Linux 5.4.87-hdrt-v0.1\"\n'
grub_str_rt+='GRUB_HIDDEN_TIMEOUT=1\n'
grub_str_rt+='GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`\n'
grub_str_rt+='GRUB_CMDLINE_LINUX=\"nosmt noapic nohalt mitigations=off hugepages=4096 nospectre_v1 nospectre_v2 spectre_v2=off nospec_store_bypass_disable nopti pti=off nvidia-drm.modeset=1 idle=poll intel_idle.max_cstate=0 processor.max_cstate=0 intel_pstate=disable numa_balancing=disable nr_cpus=8 isolcpus=1-7 cpuidle.off=1 skew_tick=1 nohz=on selinux=0 noht nosoftlockupi mce=ignore_ce nmi_watchdog=0 acpi_irq_nobalance pcie_aspm=performance rcu_nocb_poll nohz_full=1-7 rcu_nocbs=1-7 tsc=reliable clocksource=tsc hpet=disable pit=disable acpi_pm=disable\"\n'
## see kernel parameter mitigations = off

grub_str_nm='GRUB_DEFAULT=\"Advanced options for Ubuntu>Ubuntu, with Linux 5.4.206\"\n'
grub_str_nm+='GRUB_HIDDEN_TIMEOUT=1\n'
grub_str_nm+='GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`\n'
grub_str_nm+='GRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash\"'


if [ $rtgrub = 1 ]; then
    sudo bash -c "echo -e \"$grub_str_rt\" > /etc/default/grub"
else
    sudo bash -c "echo -e \"$grub_str_nm\" > /etc/default/grub"
fi

sudo update-grub

## Output GRUB file
if ! command -v cat &> /dev/null
then
    cat_command="cat"
else
    cat_command="cat"
fi
echo "New GRUB is as follow($cat_command):"
echo "#---------------------------------------------------------------------------"
$cat_command /etc/default/grub
echo "#---------------------------------------------------------------------------"


while true; do
    read -p "Do you wish to reboot to the new GRUB?" yn
    case $yn in
        [Yy]* ) sudo reboot;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no";;
    esac
done

